var express = require('express')
var router = express.Router()
var pool = require('../config/mysqlconfig')


// http://localhost:3000/info/all

//博客信息列表
router.get('/all',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
        }else{
            var sql="select * from info"
            conn.query(sql,function(error,results){
                if(error){
                    console.log("数据库连接失败")
                }
                res.send(results)
                conn.release()
            })
        }
    })
})
//按名称
// http://localhost:3000/info/select
router.post('/select',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
        }else{
            let name = req.body.i_title;
            
            console.log('name:'+name)
            let sql = "select * from info where i_title = ?"
            conn.query(sql,name,function(error,results){
                if(error){
                    console.log("查询失败")
                }
                if(results){
                    res.send(results)
                }
                conn.release()
            })
        }
    })
})
// http://localhost:3000/info/add
router.post('/add',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
        }else{
            // let id=req.body.i_id;
            let title = req.body.i_title;
            let subtitle = req.body.i_subtitle;
            let about = req.body.i_about;
            let create = new Date();
            let update = new Date();
            let sql="insert into info set ?"
            let data = {
                // i_id:id,
                i_title:title,
                i_subtitle:subtitle,
                i_about:about,
                i_createat:create,
                i_updatedat:update
            }
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log('插入失败')
                    return
                }
                if(results){
                    res.send(results)
                    conn.release()
                    
                }
                // conn.release()
            })
        }
    })
})

// http://localhost:3000/info/del
router.delete('/del',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
            return
        }else{
            let iid = req.body.id;
            let sql = 'delete from info where id=?'
            conn.query(sql,iid,function(err,results){
                if(err){
                    console.log(err)
                    return
                }
                if(results){
                    console.log(results)
                    res.send(results)
                    conn.release()
                }
            })
        }
    })
})

// http://localhost:3000/info/update

router.put('/update',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
        }else{
            let id = req.body.id;
            let title = req.body.i_title;
            let subtitle = req.body.i_subtitle;
            let about = req.body.i_about;
            let create=req.body.i_createat;
            let update=req.body.i_updatedat;
            let data = [title,subtitle,about,new Date(create),new Date(update),id]

            let sql = 'update info set i_title=?,i_subtitle=?,i_about=?,i_createat=?,i_updatedat=? where id=?'
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log("更新失败")
                    return
                }
                if(results){
                    res.redirect('all')
                    conn.release()
                }
            })
        }
    })
})

module.exports = router